<?php
require('config.php');
require('include/conexion.php');
require('include/funciones.php');

$tabla="usuarios";
if(isset($_GET['campo'])){
$campo=$_GET['campo'];
}
else{
$campo="usuario";
}


if(isset($_GET['q']) and !eregi('^ *$',$_GET['q'])){
        $q = sql_quote($_GET['q']); //para ejecutar consulta
        $busqueda = htmlentities($q); //para mostrar en pantalla

        $sqlStr = "SELECT * FROM ".$tabla." WHERE ".$campo." LIKE '%$q%'";
        $sqlStrAux = "SELECT count(*) as total FROM ".$tabla." WHERE ".$campo." LIKE '%$q%'";
    }else{
        $sqlStr = "SELECT * FROM ".$tabla;
        $sqlStrAux = "SELECT count(*) as total FROM ".$tabla;
    }

$aux = mysql_fetch_assoc(mysql_query($sqlStrAux,$link));
$query = mysql_query($sqlStr, $link);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Buscador en AJAX</title>
<script src="include/buscador.js" type="text/javascript" language="javascript"></script>
</head>

<body>
    <form action="index.php" onsubmit="return buscar()">
      <label>Buscar</label> <input type="text" id="q" name="q" value="<?php if(isset($q)) echo $busqueda;?>" onKeyUp="return buscar()">
      <select name="campo" onchange="return buscar()">
      <?php
      for($i=0;$i<mysql_num_fields($query);$i++)
                                {
                                   echo "<option value=".mysql_field_name($query,$i).">".mysql_field_name($query,$i)."</option>";
                                }
      ?>
      </select>
      <input type="submit" value="Buscar" id="boton">
      <span id="loading"></span>
    </form>
    
    <div id="resultados">
    <p><?php
        if($aux['total'] and isset($busqueda)){
                echo "{$aux['total']} Resultado".($aux['total']>1?'s':'')." que coinciden con: <strong>[ ".$busqueda." ]</strong>";
            }elseif($aux['total'] and !isset($q)){
                echo "Total de registros: {$aux['total']}";
            }elseif(!$aux['total'] and isset($q)){
                echo"No hay registros que coincidan con: <strong>[ ".$busqueda." ] </strong>";
            }
    ?></p>

    <?php 
        if($aux['total']>0){
            echo "<table border='1'>";
            echo "<tr>";
            for($i=0;$i<mysql_num_fields($query);$i++)
                                {
                                   echo "<td>".mysql_field_name($query,$i)."</td>";
                                }
            echo "</tr>";
            $total_campos=mysql_num_fields($query);
            while($row = mysql_fetch_array($query)){
          echo "<tr>";
          for($i=0;$i<$total_campos;$i++){
          echo "<td>".htmlentities($row[$i])."</td>";
                                          }
          echo "</tr>";
          }
           echo "</table>";
        }
    ?>
    </div>
</body>
</html>